package com.zte.moon.yutu;


import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 14-1-5
 * Time: 上午2:08
 * To change this template use File | Settings | File Templates.
 */
public class Detector {
    private Point point;
    private Direction direction;
    private Sensor sensor;
    /*
        C中获取数据与JAVA中获取数据的不同
        C中所有数据都是全局数据，直接获取
        JAVA中数据都在类中，获取数据就是获取类的引用，要么全局，要么传参
    */
    private Area area;
    private Point[][] map;
    private LinkedList<Point> path = new LinkedList<Point>();

    public void load(Point point, Direction direction, Area area) {
        if (point.getX() > area.getMaxX() || point.getX() < area.getMinX()
                || point.getY() < area.getMinY() || point.getY() > area.getMaxY())
            return;
        this.point = point;
        this.direction = direction;
        this.area = area;
    }

    public void turnLeft() {
        switch (this.direction) {
            case EAST:
                this.direction = Direction.NORTH;
                break;
            case NORTH:
                this.direction = Direction.WEST;
                break;
            case WEST:
                this.direction = Direction.SOUTH;
                break;
            case SOUTH:
                this.direction = Direction.EAST;
                break;
            default:
                break;
        }
    }

    public void turnRight() {
        switch (this.direction) {
            case EAST:
                this.direction = Direction.SOUTH;
                break;
            case SOUTH:
                this.direction = Direction.WEST;
                break;
            case WEST:
                this.direction = Direction.NORTH;
                break;
            case NORTH:
                this.direction = Direction.EAST;
                break;
            default:
                break;
        }
    }

    public void move() {
        switch (this.direction) {
            case EAST:
                if (this.point.getX() < this.area.getMaxX())
                    this.point.addX();
                break;
            case WEST:
                if (this.point.getX() > this.area.getMinX())
                    this.point.minusX();
                break;
            case NORTH:
                if (this.point.getY() < this.area.getMaxY())
                    this.point.addY();
                break;
            case SOUTH:
                if (this.point.getY() > this.area.getMinY())
                    this.point.minusY();
                break;
            default:
                break;
        }
    }

    public void takeAction(LinkedList<Command> commandList) {
        for (Command command : commandList) {
            switch (command) {
                case RIGHT:
                    this.turnRight();
                    break;
                case LEFT:
                    this.turnLeft();
                    break;
                case MOVE:
                    this.move();
                    break;
                default:
                    break;
            }
        }
    }

    public String toString() {
        return this.point.toString() + " " + this.direction.tostring();
    }

    public void setSensor(Sensor sensor) {
        this.sensor = sensor;
    }

    public void findPits() {
        //构造map
        this.map = new Point[this.area.getMaxX() - this.area.getMinX()][this.area.getMaxY() - this.area.getMinY()];
        for (int i = 0; i < this.area.getMaxX() - this.area.getMinX(); i++) {
            for (int j = 0; j < this.area.getMaxY() - this.area.getMinY(); j++) {
                this.map[i][j] = new Point(this.area.getMinX() + i, this.area.getMinY() + j);
            }
        }

        //选点
        Point pickedPoint = pickPoint();
        if (null == pickedPoint) {
            //选点结束
            return;
        }



    }



    private Point pickPoint() {
        //把难以构造的，变为筛选符合要求
        int c = 0;
        for (int i = 0; i < this.area.getMaxX() - this.area.getMinX(); i++) {
            for (int j = 0; j < this.area.getMaxY() - this.area.getMinY(); j++) {
                if (Math.abs(map[i][j].getX() - this.point.getX()) <= c &&
                        Math.abs(map[i][j].getY() - this.point.getY()) <= c) {
                    return map[i][j];
                }
            }
        }
        return null;
    }

}
